package com.example.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.UUID;

import javax.annotation.Resource;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.Base64Utils;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {
	
	private String demo = "11111111111111111111";
	
	@Resource(name="print")
	public String word;
	
	@RequestMapping("/test")
	public String test() {
//		return "hh";
		return word;
	}
	
	@Bean(name = "testImport")
	public String testImport(test t) {
		return t.print();
	}
	
	public DemoApplication() {
		System.out.println("start");
	}
	
	public String ss() {return demo;}

	private final static String WARN_URL = "https://oapi.dingtalk.com/robot/send?access_token=e487935f9bc415ebdc8b9dcc68cd06d128fecdb3c694d03a6e4535a9471b07c8";

	public static void send(String warnMessage) throws ApiException {

		DingTalkClient client = new DefaultDingTalkClient(WARN_URL);
		OapiRobotSendRequest request = new OapiRobotSendRequest();
		request.setMsgtype("text");
		OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
		text.setContent(warnMessage);
		request.setText(text);
		OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
		at.setAtUserIds(Arrays.asList("4525xxxxxxxxx7041"));
		at.setIsAtAll(false);
		request.setAt(at);
		OapiRobotSendResponse response = client.execute(request);
		System.out.println(response.getBody());
	}
	

	public static void main(String[] args) throws Exception {
		System.out.println("123123");
		send("test告警提醒");

//		AlternativeJdkIdGenerator alternativeJdkIdGenerator = new AlternativeJdkIdGenerator();
//		UUID uuid = alternativeJdkIdGenerator.generateId();
//		System.out.println(alternativeJdkIdGenerator.generateId());
//		System.out.println(alternativeJdkIdGenerator.generateId());
//		System.out.println(alternativeJdkIdGenerator.generateId());
//		System.out.println(alternativeJdkIdGenerator.generateId());
//		System.out.println(Base64Utils.encodeToString("123456".getBytes()));
//		String str = new String(Base64Utils.decodeFromString("MTIzNDU2"));
//		System.out.println(str);
//
//		// 读取class
//		Class<?> forName = ClassUtils.forName(DemoApplication.class.getName(), DemoApplication.class.getClassLoader());
//		DemoApplication newInstance = (DemoApplication)forName.newInstance();
//
//		Method method = ClassUtils.getMethod(newInstance.getClass(), "ss");
//		System.out.println("ClassUtils: "+method.invoke(newInstance));
//
//		System.out.println("newInstance.ss(): "+newInstance.ss());
//
//		Field declaredField = forName.getDeclaredField("demo");
//		declaredField.setAccessible(true);
//		declaredField.set(newInstance, "222222222222222222222222222222");
//		System.out.println("declaredField.get(newInstance): "+declaredField.get(newInstance));
//
//		Method[] declaredFields = forName.getDeclaredMethods();
//		for (Method field : declaredFields) {
//			System.out.println("Method: "+field.getName());
//			if(field.getReturnType().getName().equals("java.lang.String")) {
//				if(field.getParameterCount() == 0) {
//					System.out.println(field.invoke(newInstance));
//				}
//			}
//		}
//		System.out.println(DemoApplication.class.getName());
//
////		SpringApplication.run(DemoApplication.class, args);
//
//		System.out.println("---------------------------------------------------------");
//
//		testProxy.init(DemoApplication.class, newInstance);
//
//		System.out.println(newInstance.tm.select(1, 2));
//		System.out.println(newInstance.t.print());
	}
	
	@testProxy.Autowired
	testMapper tm;

	@testProxy.Autowired
	test t;
	
}
